package com.hbvtc.xxxy.nikeSale.frames;

/*
 * 删除商品信息
 */
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.hbvtc.xxxy.nikeSale.daos.GoodsDao;
import com.hbvtc.xxxy.nikeSale.utils.DbManager;

public class DelShopNike extends JFrame {
	JLabel lalId;
	JTextField txtId;
	JButton btnDel;
	Connection conn;
	PreparedStatement pst;
	ResultSet rs;
	String sql;

	public DelShopNike() {
		this.setTitle("删除商品信息界面");
		this.setSize(1000, 700);
		this.setLocationRelativeTo(null);
		this.setVisible(true);

		ImageIcon imageIcon = new ImageIcon(getClass().getResource("01.png"));
		this.setIconImage(imageIcon.getImage());

		String path = "images/001.jpg";
		ImageIcon background = new ImageIcon(path);
		JLabel label = new JLabel(background);
		label.setBounds(0, 0, this.getWidth(), this.getHeight());
		JPanel imagePanel = (JPanel) this.getContentPane();
		imagePanel.setOpaque(false);
		this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));

		lalId = new JLabel("请输入删除的商品编号:");
		txtId = new JTextField();
		btnDel = new JButton("立即删除");
		this.add(lalId);
		this.add(txtId);
		this.add(btnDel);
		this.setLayout(null);
		lalId.setBounds(140, 170, 250, 60);
		lalId.setFont(new Font(null, 0, 24));
		txtId.setBounds(450, 180, 220, 40);
		txtId.setFont(new Font(null, 0, 24));
		btnDel.setBounds(550, 450, 170, 40);
		btnDel.setFont(new Font(null, 0, 24));

		btnDel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int id = Integer.parseInt(txtId.getText().trim());
				// 实例化商品信息类
				final GoodsDao goodsDao = new GoodsDao();
				conn = DbManager.getconn();
				sql = "delete from goods where  GoodsID=?;";
				try {
					pst = conn.prepareStatement(sql);
					pst.setInt(1, id);
					//调用商品信息类里面的getByGoodsId(id)方法--查询
					if (goodsDao.getByGoodsId(id) == false) {
						pst.execute();
						JOptionPane.showMessageDialog(DelShopNike.this,
								"删除商品成功");
					} else {
						JOptionPane.showMessageDialog(DelShopNike.this,
								"删除商品失败");
					}
				} catch (HeadlessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
	}

	public static void main(String[] args) {
		new DelShopNike();
	}
}
